package com.heu.blood.transfusion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.transfusion.entity.BloodAdverseReactionEntity;
import com.heu.blood.transfusion.entity.TransfusionReviewEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.transfusion.dao.TransfusionReviewDao;
import com.heu.blood.transfusion.entity.TransfusionReviewEntity;
import com.heu.blood.transfusion.service.TransfusionReviewService;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("transfusionReviewService")
@Slf4j
public class TransfusionReviewServiceImpl extends ServiceImpl<TransfusionReviewDao, TransfusionReviewEntity> implements TransfusionReviewService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 构造TransfusionReviewEntity对象
        TransfusionReviewEntity transfusionReview = new TransfusionReviewEntity(); // 用于接收传递过来的TransfusionReviewEntity参数
        if (Strings.isNotBlank((String) params.get("transfusionId"))){
            transfusionReview.setTransfusionId((String) params.get("transfusionId")); // 添加transfusionId
        }
        if (Strings.isNotBlank((String) params.get("reviewer"))){
            transfusionReview.setReviewer((String) params.get("reviewer")); // 添加reviewer
        }
        if (Strings.isNotBlank((String) params.get("status"))){
            transfusionReview.setStatus((String) params.get("status")); // 添加reviewer
        }

        // 时间格式化，解决Date->String 报错
        String startTime = (String)params.get("startTime");
        String endTime = (String) params.get("endTime");


        // 构造条件构造器对象
        LambdaQueryWrapper<TransfusionReviewEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.eq(Strings.isNotBlank(transfusionReview.getTransfusionId()), TransfusionReviewEntity::getTransfusionId, transfusionReview.getTransfusionId())
                .eq(Strings.isNotBlank(transfusionReview.getReviewer()), TransfusionReviewEntity::getReviewer, transfusionReview.getReviewer())
                .eq(Strings.isNotBlank(transfusionReview.getStatus()), TransfusionReviewEntity::getStatus, transfusionReview.getStatus())
                .eq(Strings.isNotBlank(String.valueOf(transfusionReview.getIfShow())), TransfusionReviewEntity::getIfShow,IF_SHOW.code())
                .ge(Strings.isNotBlank(startTime), TransfusionReviewEntity::getReviewTime, startTime)
                .le(Strings.isNotBlank(endTime),TransfusionReviewEntity::getReviewTime, endTime);

        // 根据输血申请单号升序排列
//        queryWrapper.orderByAsc(TransfusionReviewEntity::getTransfusionId);
//        IPage<TransfusionReviewEntity> page = this.page(
//                new Query<TransfusionReviewEntity>().getPage(params),
//                queryWrapper
//        );
        //新增结果查询排在前面
        queryWrapper.orderByDesc(TransfusionReviewEntity::getGmtCreate);
        IPage<TransfusionReviewEntity> page = this.page(
                new Query<TransfusionReviewEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


}